<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class Mydoctor extends Model
{
    protected $table = 'c_mydoctors';

    public function doctor()
    {
        return $this->hasMany('App\Models\Doctor', 'id', 'doctor_id');
    }
    public function doctor_info()
    {
        return $this->hasOne('App\Models\Doctor', 'id', 'doctor_id');
    }

    /****
     * @param $filter
     * @return int
     * 添加我的医护
     */
    public function addMyDoctor($filter)
    {
        $info = $this->getInfos($filter);
        $doctor = $this->getDoctor($filter);
        if (!empty($info)) { //取消收藏

            $res = $this->where("doctor_id", '=', $filter['doctor_id'])->where("user_id", '=', $filter['user_id'])->delete();
            if ($res) {
                //减掉粉丝数
                (new DoctorWorks())->where([['id', '=', $doctor->work_id]])->decrement("fans_num");
                (new Doctor())->where([['id', '=', $filter['doctor_id']]])->decrement("fans_num");
                return 1;
            } else {
                return 2;
            }
        } else {
            $this->doctor_id = $filter['doctor_id'];
            $this->user_id = $filter['user_id'];
            $res = $this->save();
            if ($res) {
                //增加粉丝
                (new DoctorWorks())->where([['id', '=', $doctor->work_id]])->increment("fans_num");
                (new Doctor())->where([['id', '=', $filter['doctor_id']]])->increment("fans_num");
                return 3;
            } else {
                return 4;
            }
        }
    }

    public function getInfos($data)
    {
        return $this->where("doctor_id", '=', $data['doctor_id'])->where("user_id", '=', $data['user_id'])->first();
    }

    public function getDoctor($data)
    {
        return (new Doctor())->where("id", '=', $data['doctor_id'])->first();
    }

    /***
     * @param $param
     * 获取我的医师
     */
    public function getMyDoctorList($param)
    {
        $data = $this->with(['doctor_info'])->where("user_id", '=', $param['user_id'])->get();
        if (!empty($data)) {
            $data = $data->toArray();
            $host = (new Url());
            $professional =  new Professional();
            $clinic =  new ClinicCategory();
            $order = new Order;
//            $doc = new Doctor();
            foreach ($data as $k => $v) {
                if (!empty($v['doctor_info'])) {
                    $data[$k]['doctor_info']['clinic_name'] = $clinic->where([['id', '=', $v['doctor_info']['clinic_id']]])->value('classify_name');
                    $data[$k]['doctor_info']['profile'] = $host::getHttpHost() . 'upload/' . $v['doctor_info']['profile'];
                    $data[$k]['doctor_info']['front_card'] = $host::getHttpHost() . 'upload/' . $v['doctor_info']['front_card'];
                    $data[$k]['doctor_info']['behind_card'] = $host::getHttpHost() . 'upload/' . $v['doctor_info']['behind_card'];
                    //职称
                    $profe = $professional->where([['id', '=', $v['doctor_info']['professional_id']]])->first();
                    if ($profe) {
                        $data[$k]['doctor_info']['professional'] = $profe->toArray();
                    }else{
                        $data[$k]['doctor_info']['professional'] = null;
                    }
                    $data[$k]['doctor_info']['consult_num'] = $order->join('c_order_goods', 'c_orders.id', '=', 'c_order_goods.order_id')
                        ->where([['c_order_goods.goods_id','=',$v['doctor_info']['id']],['c_orders.order_status','=',1]])->count("c_orders.id");
                }else{
                    unset($data[$k]);
                    //删除关注记录
                    $this::destroy($v['id']);
                }
            }
        }
        if (!empty($data)) {
            return $data;
        }
        return [];
    }
}
